<?php
require_once '../mvc/shoutbox_view.php';
require_once '../include/mysql_config.php';
require_once '../include/mysql_connection.php';

class ShoutboxController {
	private $shoutbox_view;

	function __construct(){
		$this->shoutbox_view = new ShoutboxView();
	}
	
	function get_nick( $id ){
		$query = "SELECT Op_nick FROM Operacion WHERE Op_id = $id";
		$result = $GLOBALS['mysql_connection']->query( $query);
		
		$row = $result->fetch_array();
		return $row['Op_nick'];
	}

	function GetShouts( $usuario ){
		$destinatario = $usuario->get_id();
		$query = "SELECT * FROM Shoutbox WHERE Shout_dest = '$destinatario'";
		$result = $GLOBALS['mysql_connection']->query( $query);

		$Shouts = '';
		while ($row = $result->fetch_array()){
			$remitente = $row['Shout_rem'];
			$post = $row['Shout_post'];
			$Shouts .= '<li>' .$this->get_nick($remitente). ':  ' .$post. '</li>';
		}
		return $Shouts;
	}

	function display($usuario ){
		$Shoutbox = $this->GetShouts( $usuario );
		return $this->shoutbox_view->display($Shoutbox);
	}
	
	function NewPost( $dest, $rem, $msg ){
		$sql = "INSERT INTO Shoutbox SET Shout_rem = '$rem' , Shout_dest = '$dest' , Shout_post = '$msg' , Shout_carac = 1"; 
		$result = $GLOBALS['mysql_connection']->query( $query);
	} 

}
?>
